C program to implement Factorial program

Simple C program to implement Factorial program using Recursive and Iterative Methods

Code:

#include<stdio.h>

long int fact(int n);

long int Ifact(int n);

 

int main( )

{

            int num;

            printf("Enter a number : ");

            scanf("%d", &num);

 

            printf("\nUsing Recursion :: \n");

            if(num<0)

                        printf("No factorial for negative number\n");

            else

                        printf("Factorial of %d is %ld\n", num, fact(num) );

 

    printf("\nUsing Iterative :: \n");

 

            if(num<0)

                        printf("No factorial for negative number\n");

            else

                        printf("Factorial of %d is %ld\n", num, Ifact(num) );

 

                        return 0;

}/*End of main()*/

 

/*Recursive*/

long int fact(int n)

{

            if(n == 0)

                        return(1);

            return(n * fact(n-1));

}/*End of fact()*/

 

/*Iterative*/

long int Ifact(int n)

{

            long fact=1;

            while(n>0)

            {

                        fact = fact*n;

                        n--;

            }

            return fact;

}


Comments

Popular posts from this blog

C program to evaluate Prefix Expression using Stack data structure

C++ program to perform data transformation Min-max and Z score Normalization

Java Program to Implement sorting algorithm using TCP on Server application